package JUnit;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RunnableClient implements Runnable, RunnableClientInterface{
	private String _result;
	private String _msg;
	protected void set_msg(String _msg) {
		this._msg = _msg;
	}

	private String _host;
	private int _port;
	private boolean _isResult;
public RunnableClient(String host,  int port){
	this._host=host;
	this._port=port;
	this._isResult=false;
}

public RunnableClient(String host,  int port, String msg){
	this._host=host;
	this._port=port;
	this._isResult=false;
	this._msg=msg;
}
	@Override
	public String process(String url, Map<String, String> headers, String msg) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void run() {
		try {
			sendHttpToServer(this._msg);
		} catch (IOException| InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public synchronized boolean hasResponse() {
		return this._isResult;
	}

	@Override
	public synchronized String getResponse() {
		return this._result;
	}
	
	private void sendHttpToServer(String msg)
			throws UnsupportedEncodingException, IOException,
			InterruptedException {
		String res = new String();
		System.out
				.println("**************************************************************");
		System.out.println("Connecting to " + _host + ":" + _port);

		Socket clientSocket = null;
		PrintWriter out = null;
		// Trying to connect to a socket and initialize an output stream
		try {
			clientSocket = new Socket(_host, _port); // host and port
			out = new PrintWriter(clientSocket.getOutputStream(), true);
		} catch (UnknownHostException e) {
			System.out.println("Unknown host: " + _host);
			System.exit(1);
		} catch (IOException e) {
			System.out
					.println("Couldn't get output to " + _host + " connection");
			System.exit(1);
		}

		BufferedReader in = null;
		// Initialize an input stream
		try {
			in = new BufferedReader(new InputStreamReader(
					clientSocket.getInputStream(), "UTF-8"));
		} catch (IOException e) {
			System.out.println("Couldn't get input to " + _host + " connection");
			System.exit(1);
		}

		System.out.println("sending to server:");

		System.out.println(msg);

		BufferedReader userIn = new BufferedReader(new InputStreamReader(
				System.in, "UTF-8"));
		msg = URLEncoder.encode(msg, "utf-8");
		System.out
				.println("----------------SERVER RESPONE:----------------------");

		out.println(msg);
		String line;
		while ((line = in.readLine()) != null) {
			try{
			res += (URLDecoder.decode(line, "utf-8"))+"\n";
			String ResultString = null;
				Pattern regex = Pattern.compile("SESSIONID=.*", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
				Matcher regexMatcher = regex.matcher(res);
				if (regexMatcher.find()) {
					ResultString = regexMatcher.group();
				} 
			
				if (ResultString!= null){
					
				//	System.out.println(ResultString);
					testClient.setCookie(ResultString);
				}
			}
			catch(Exception e){
			}
		}
		userIn.close();
		// Close all I/O
		out.close();
		in.close();

		clientSocket.close();
		_result=res;
		_isResult=true;
	}

}
